package com.yeyks.app.config;

import com.mongodb.MongoClientOptions;
import com.panfeiyun.project.tools.monitor.common.autoconfigure.annotation.PFYEnableSpringUtil;
import com.panfeiyun.project.tools.monitor.common.autoconfigure.config.MongodbStorageConfig;
import com.panfeiyun.project.tools.monitor.common.properties.PFYMonitorUtilMongoPropertiesCollection;
import com.panfeiyun.project.tools.monitor.request.all.autoconfigure.annotation.PFYEnableRequestAllController;
import com.panfeiyun.project.tools.monitor.request.urlStatistics.aop.RequestStatisticsAspect;
import com.panfeiyun.project.tools.monitor.request.urlStatistics.utils.ApplicationContextUtil;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.mongo.MongoClientFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.core.env.Environment;
import org.springframework.data.mongodb.MongoDbFactory;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.SimpleMongoDbFactory;
import org.springframework.data.mongodb.core.convert.DefaultDbRefResolver;
import org.springframework.data.mongodb.core.convert.DefaultMongoTypeMapper;
import org.springframework.data.mongodb.core.convert.MappingMongoConverter;
import org.springframework.data.mongodb.core.mapping.MongoMappingContext;
import org.springframework.data.mongodb.repository.config.EnableMongoRepositories;

@Configuration
@ConditionalOnProperty(prefix = "pfy.monitor.data.mongodb.storage", name = "host")
@PFYEnableSpringUtil
@PFYEnableRequestAllController
@Import({MongodbStorageConfig.class})
public class PFYMonitorUtilConfig {

    @Configuration
    @EnableMongoRepositories(
            basePackages = "com.panfeiyun.project.tools.monitor.common.dao.mongodb"
            , mongoTemplateRef = "pfyStorageMongoTemplate"
    )
    public class MongodbConfig {

        @Bean("pfyMonitorUtilMongoPropertiesCollection")
        public PFYMonitorUtilMongoPropertiesCollection pfyMonitorUtilMongoPropertiesCollection() {
            return new PFYMonitorUtilMongoPropertiesCollection();
        }

        @Bean("pfyStorageMongoClientFactory")
        public MongoClientFactory pfyStorageMongoClientFactory(
                @Qualifier("pfyMonitorUtilMongoPropertiesCollection") PFYMonitorUtilMongoPropertiesCollection pfyMonitorUtilMongoPropertiesCollection
                , Environment environment) {
            return new MongoClientFactory(pfyMonitorUtilMongoPropertiesCollection.getStorage(), environment);
        }

        @Bean("pfyStorageMongoClientOptions")
        public MongoClientOptions pfyStorageMongoClientOptions(ObjectProvider<MongoClientOptions> options) {
            return MongoClientOptions.builder().build();
        }

        @Bean("pfyStorageMongoDbFactory")
        public MongoDbFactory pfyStorageMongoDbFactory(
                @Qualifier("pfyMonitorUtilMongoPropertiesCollection") PFYMonitorUtilMongoPropertiesCollection pfyMonitorUtilMongoPropertiesCollection
                , @Qualifier("pfyStorageMongoClientFactory") MongoClientFactory pfyStorageMongoClientFactory
                , @Qualifier("pfyStorageMongoClientOptions") MongoClientOptions pfyStorageMongoClientOptions) throws Exception {
            return new SimpleMongoDbFactory(pfyStorageMongoClientFactory.createMongoClient(pfyStorageMongoClientOptions), pfyMonitorUtilMongoPropertiesCollection.getStorage().getDatabase());
//        return new SimpleMongoDbFactory(new MongoClient(storageMongoProperties.getHost(), storageMongoProperties.getPort()),storageMongoProperties.getDatabase());
        }

        @Bean("pfyStorageMongoTemplate")
        public MongoTemplate pfyStorageMongoTemplate(@Qualifier("pfyStorageMongoDbFactory") MongoDbFactory pfyStorageMongoDbFactory) throws Exception {

            //remove _class
            MappingMongoConverter converter = new MappingMongoConverter(new DefaultDbRefResolver(pfyStorageMongoDbFactory), new MongoMappingContext());
            converter.setTypeMapper(new DefaultMongoTypeMapper(null));

            return new MongoTemplate(pfyStorageMongoDbFactory, converter);
        }

    }

    @Configuration
//    @EnableMongoRepositories(
//            basePackages = {"com.panfeiyun.project.tools.monitor.common.dao.mongodb"},
//            mongoTemplateRef = "pfyStorageMongoTemplate"
//    )
    @ComponentScan({"com.panfeiyun.project.tools.monitor.request.urlStatistics.service"})
    @Import({ApplicationContextUtil.class, RequestStatisticsAspect.class})
    public class MongodbUrlStatistics {

    }

}
